<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="角色" prop="RoleID">
                <el-select v-model="ruleForm.RoleID" placeholder="请选择角色">
                    <el-option v-for="(item, index) in roles" :key="index" 
                    :label="item.RoleName" :value="item.RoleID"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="头像" prop="ImgID">
                <el-select v-model="ruleForm.ImgID" placeholder="请选择头像">
                    <el-option v-for="(item, index) in images" :key="index" 
                     :label="item.Title" :value="item.ImgId">
                        <img style="width: 30px;height: 30px;vertical-align: text-bottom;" :src="imgUrl + item.Path">
                        {{item.Title}}
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="用户名" prop="LoginName">
                <el-input v-model="ruleForm.LoginName"></el-input>
            </el-form-item>
            <el-form-item label="真实姓名" prop="UserName">
                <el-input v-model="ruleForm.UserName"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="Sex">
                <el-radio-group v-model="ruleForm.Sex">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
                </el-radio-group>
            </el-form-item>
            
            <el-form-item label="手机号码" prop="Phone">
                <el-input v-model="ruleForm.Phone"></el-input>
            </el-form-item>
            <el-form-item label="出生日期" required>
                <el-form-item prop="Birthday" style="width: 200px;">
                    <el-date-picker type="date" 
                    placeholder="选择出生日期" 
                    v-model="ruleForm.Birthday"
                    style="width: 100%;"></el-date-picker>
                </el-form-item>

            </el-form-item>
            <el-form-item label="状态" prop="Status">
                <el-switch v-model="ruleForm.Status"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import base from '@/plugins/base'

export default {
    name: 'UsersCreate',
    data() {
        return {
            roles:[],
            images:[],
            imgUrl: '',


            

            ruleForm: {
                UserID: 0,
                UserName: '伊娃',
                Sex: '男',
                ImgID: 2,
                RoleID: 3,
                Birthday: new Date('1999-09-19'),
                Phone: '18512345678',
                LoginName: 'yiwa',
                LoginPwd:'123',  
                Status: true,
                LastTime: new Date()
            },

            rules: {
                UserName: [
                    { required: true, message: '请输入真实姓名', trigger: 'blur' },
                    { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
                ],
                LoginName: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
                ],
                RoleID: [
                    { required: true, message: '请选择角色', trigger: 'change' }
                ],
                Birthday: [
                    { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
                ],
                Phone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在为11位数字', trigger: 'blur' }
                ],
            }
        }
    },
    props:{
        dialogVisible:Boolean
    },
    methods: {

        //提交表单
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //axios 提交
                    //this.ruleForm
                    console.log('this.ruleForm: ', this.ruleForm);

                    this.axios({
                        method:'post',
                        url:'Users',
                        params:{},
                        data:this.ruleForm
                    }).then(res=>{
                        console.log('res: ', res);
                        if(res.data.code>0){
                            this.$message({
                                message:res.data.msg,
                                type:'success'
                            });
                            ///* 改变父窗口的值来关闭弹窗 */
                            this.$emit('update:dialogVisible',false);
                            // 触发刷新
                            this.$emit('refresh') 

                        }else{
                            this.$message({
                                message:res.data.msg,
                                type:'error'
                            })
                        }
                    })
                    .catch(err=>{
                        console.log('err: ', err);

                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        //重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created() {
        this.imgUrl = base.imgUrl;

        this.axios({
            method:'get',
            url:'Roles'
        }).then(res=>{
            console.log('res: ', res);
            this.roles = res.data.data;
            
        })

        this.axios({
            method:'get',
            url:'Images'
        }).then(res=>{
            console.log('res: ', res);
            this.images = res.data.data;
            
        })
    },
}
</script>

<style scoped></style>